<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Product_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
    }
    public $table = 'tbl_product';

    public function index() {
        $this->view();
    }

    public function view() {
        $data["title"] = "Product";
        $data['data_list'] = $this->selectModel->selectData($this->table);
        $this->load->view('panel/product/list', $data);
    }



function show_list ( $offset = NULL ) {
        $employee_id             = $this->session->userdata ('user_id');
        $data['select_inv_shop'] = $this->mod_general->check_permission ($employee_id, 'select_inv_shop');
        $this->load->model ('general/mod_general');

        $country          = $this->session->userdata ("country_id");
        $shop_location_id = $this->session->userdata ("shop_id");

        if ( $this->uri->segment (4) ) {
            $shop_location_id = $this->uri->segment (4);
        }

//        if ($this->uri->segment(4)){
//            $this->session->set_userdata("tmp_shop_id", $this->uri->segment(4));
//            $shop_location_id = $this->session->userdata('tmp_shop_id');
//        }else {
//            if($this->session->userdata('tmp_shop_id')) {
//                $shop_location_id = $this->session->userdata('tmp_shop_id');
//            }
//        }

        if ( !is_null ($offset) ) {
            $offset = $this->uri->segment (5);
        }

        //Configure pagination
        $this->load->library ('pagination');
        $this->load->helper ('widget');
        $limit      = 15;
        $segment    = 5;
        $num_link   = 10;
        $total_rows = $this->mod_inventory->total_rows ($shop_location_id, $country, 6);
        $base_url   = site_url (num_segment (3) . intval ($shop_location_id));
        pagination ($limit, $segment, $base_url, $total_rows, $num_link);


        $data['shop_id']        = $shop_location_id;
        $data['inventory_list'] = $this->mod_inventory->get_inventory ($limit, $offset, $shop_location_id);
        $data['links']          = $this->pagination->create_links ();
        $data['shoplocation']   = $this->mod_general->get_shop_location ($country);
        $data['selected_shop']  = $this->mod_general->check_permission ($employee_id, "inv_shop_select");
        //$this->data['transferList'] = $this->mod_inventory->get_transfer();
        $data['select_shop']    = $this->mod_inventory->rec_select_shop ();
        $data['page_title']     = "Inventory Management";
        $this->load->view ("site/main_site", $data);
    }




    # to add data
    public function add() {
        $data["title"] = "Product";

        # code...get_main_category
        $table1 = "tbl_main_category";
        $data['main_category_list'] = $this->selectModel->selectData($table1);


        # code...get_main_category
        $table2 = "tbl_category";
        $data['category_list'] = $this->selectModel->selectData($table2);


        # code...get_main_category
        $table3 = "tbl_sub_category";
        $data['sub_category_list'] = $this->selectModel->selectData($table3);

        if(isset($_POST['save'])){
            $title = $_POST['Title'];
            $fields = array("ProductName", "Enable");
            $values = array($title, 1);
            $data['data_add'] = $this->addModel->addData($this->table, $fields, $values);
            if($data['data_add']){
                $this->session->set_userdata('msg', alert_success('Product add '));
            }else{
                $this->session->set_userdata('msg', alert_error('Product add '));
            }
            redirect('admin_panel/product/view');
        }
        $this->load->view('panel/product/add', $data);
    }

    # to update data
    public function update($val) {
        $field = "ProductID";
        $data["title"] = "Product";
        $data['data_list'] = $this->selectModel->selectDataWhere($this->table, $field, $val);
        if(isset($_POST['update'])){
            echo $val;
            exit;
            $Title = $_POST['Title'];
            $fields = array("ProductName");
            $values = array($Title);
            $data['data_update'] = $this->updateModel->updateData($this->table, $fields, $values, $field, $val);
            if($data['data_update']){
                $this->session->set_userdata('msg', 'You got successful!!');
            }else{
                $this->session->set_userdata('msg', 'You are fail!!');
            }
            redirect('admin_panel/product/view');
        }
        $this->load->view('panel/product/edit', $data);
    }

    # to delete data
    public function delete($val) {
        #code here
        $field = "Enable";
        $val = 0;
        $data['delete'] = $this->deleteModel->deleteData($this->table, $field, $val);
        if($data['delete']){
            $this->session->set_userdata('msg', 'You got successful!!');
        }else{
            $this->session->set_userdata('msg', 'You are fail!!');
        }
        redirect('admin_panel/product/view');
    }

}

/* End of file main_category_controller.php */
/* Location: ./application/controllers/panel/main_category_controller.php */